package marek.barak.classes;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Date;

public class ConnectionData {
	private static File dLocation;
	private static ConnectionData con;
	private String fileName;
	
	static{
		dLocation=new File("."+File.separator+"Data");
		if(!dLocation.exists())
			dLocation.mkdir();	
	}
	public static ConnectionData getInstance(){
		if (con==null){
			con = new ConnectionData();
		}
		return con;
	}
	
	@SuppressWarnings("deprecation")
	private ConnectionData(){
	Date dnes = new Date();
	fileName =Integer.toString(dnes.getDate())+"_"+Integer.toString(dnes.getMonth())+"_"+Integer.toString(dnes.getYear()) +".od";
	}
	public List<Data> getData(Date from, Date to){
		
		return null;
	}
	public void saveData(Data d) throws FileNotFoundException, IOException{
		ObjectOutputStream out;
		try{
			File today = new File(dLocation+File.separator+fileName);
			System.out.println(today.getAbsolutePath());
			out = new ObjectOutputStream(new FileOutputStream(today.getAbsolutePath()));
			out.writeObject(d);
			System.out.println("Write");
		}finally{
			
		}
			
	}
		
	public Data getData() throws FileNotFoundException, IOException, ClassNotFoundException{
		File today = new File(dLocation.getAbsolutePath()+File.separator+fileName);
		Data data = null;
		if(today.exists()){
			ObjectInputStream in= null;
			in = new ObjectInputStream(new FileInputStream(today.getAbsolutePath()));
			data = (Data)in.readObject();
			System.out.println();
		}
		return data;
	}
}
